<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/14
 * Time: 15:46
 */

namespace app\admin\controller;


use think\admin\Controller;

/**
 * 客流统计
 * Class GuestFlow
 * @package app\admin\controller
 */
class GuestFlow extends Controller
{
    /**
     * 人脸识别表
     * @var string
     */
    protected $table = 'lg_face_recognition';

    /**
     * 客流统计列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function index()
    {
        $this->title = '客流统计';
        $data = $this->request->param();
        $query = $this->_query($this->table)->alias('fr')
            ->join('lg_distributor d', 'fr.shop_id=d.id', 'LEFT')
            ->join('lg_equip e', 'e.equip_id=fr.equipment_id and e.channel_id=fr.channel_id', 'LEFT')
            ->field('fr.*,d.name shop_name,e.is_come')
            ->order('fr.id DESC');
        if (isset($data['shop_id']) && $data['shop_id'] !== '') {
            $query->where('fr.shop_id', $data['shop_id']);
        }
        if (isset($data['dateTime']) && !empty($data['dateTime'])) {
            switch ($data['dateTime']) {
                case 'day':
                    $query->whereTime('fr.enter_time', 'today');
                    break;
                case 'week':
                    $query->whereWeek('fr.enter_time');
                    break;
                case 'month':
                    $query->whereMonth('fr.enter_time');
                    break;
                case 'yesterday':
                    $query->whereTime('fr.enter_time', 'yesterday');
                    break;
                case 'last_week':
                    $query->whereTime('fr.enter_time', 'last week');
                    break;
                case 'last_month':
                    $query->whereTime('fr.enter_time', 'last month');
                    break;
                default:
                    $query->whereTime('fr.enter_time', 'today');
            }
        }
        if (isset($data['is_filter']) && $data['is_filter'] === 1) {
            $query->group('fr.face_id');
        }
        if (isset($data['is_come']) && $data['is_come'] !== '') {
            $query->where('e.is_come', $data['is_come']);
        }
        $query->page();
    }

    /**
     * 列表数据处理
     * @param $list
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(&$list)
    {
        $shopList = $this->app->db->name('lg_distributor')->where('status', 1)->field('id,name')->select();
        $this->assign([
            'shopList' => $shopList
        ]);
    }
}